#include "ticket.h"
#include "time.h"
#include <iostream>
#include "order.h"
#include <fstream>
#include <sstream>
using std::cout, std::cin, std::endl, std::string, std::ofstream, std::ifstream, std::stringstream, std::cerr;

Ticket::Ticket(const string& _code, const string& _departure, 
    const string& _arrival, const Date& _dprtime, const Date& _arvtime, 
    const int _price, const int _remain)
    : code(_code), departure(_departure), arrival(_arrival), 
    dprtime(_dprtime), arvtime(_arvtime), price(_price), remain(_remain)
    {


    }

void Ticket::showTicketInline(){
    cout << "车次: " << code 
    << " | " << "始发地: " << departure 
    << " | " << "目的地: " << arrival
    << " | " << "发车时间: " << dprtime     //需要写运算符重载
    << " | " << "到达时间: " << arvtime
    << " | " << "价格: " << price
    << " | " << "余票: " << remain<< endl;
}

void Ticket::showTicketInblock(){
    cout << "车次: " << code
    << "\n" << "始发地: " << departure 
    << "\n" << "目的地: " << arrival
    << "\n" << "发车时间: " << dprtime     //需要写运算符重载
    << "\n" << "到达时间: " << arvtime
    << "\n" << "价格: " << price
    << "\n" << "余票: " << remain << endl;
}

void TicketList::showAllTickets(){
    for(auto train = tickets.begin(); train != tickets.end(); ++train){
        train->showTicketInline();
    }
}

bool TicketList::isEmpty(){
    return tickets.empty();
}

//增
void TicketList::addTicket(const Ticket& ticket){
    tickets.push_back(ticket);
}

void TicketList::addTicket(){  
    cout << "请输入要添加的车次:";
    string code;
    cin >> code;
    if(auto ticket = codeSearch(code)){
        cout << "车次已存在!" << endl;
    }else{
        cout << "请输入始发地:";
        string departure;
        cin >> departure;
        cout << "请输入目的地:";
        string arrival;
        cin >> arrival;
        cout << "请输入价格:";
        int price;
        cin >> price;
        cout << "请输入余票:";
        int remain;
        cin >> remain;
        cout << "请输入发车时间:";
        Date dprtime;
        dprtime.setDate();
        cout << "请输入到达时间:";
        Date arvtime;
        arvtime.setDate();
        if(dprtime.dateLaterThan(arvtime)){
            cout << "发车时间不能晚于到达时间!" << endl;
        }else{
            tickets.push_back(Ticket(code, departure, arrival, dprtime, arvtime, price, remain));
            cout << "添加成功!" << endl;
        }
    }
}

//删
bool TicketList::removeTicket(){
    string code;
    cout << "请输入要删除的车次:";
    cin >> code;
    for(auto train = tickets.begin(); train != tickets.end(); ++train){
        if(train->getCode()==code){
            tickets.erase(train);
            cout << "删除成功!" << endl;
            return true;
        }
    }
    cout << "未找到相应车次!" << endl;
    return false;
}

//改
bool TicketList::modifyTicket(){
    string code;
    cout << "请输入要修改的车次:";
    cin >> code;
    if(auto ticket = this->codeSearch(code)){
        ticket->showTicketInblock();   //显示车次信息
        cout << "请输入要修改的车次信息(departure, arrival, dprtime, arvtime, price, remain):";
        string input;
        cin >> input;
        if(input == "departure"){
            cout << "请输入新的始发地:";
            string departure;
            cin >> departure;

            cout << "修改成功!" << endl;
        }else if(input == "arrival"){
            cout << "请输入新的目的地:";
            string arrival;
            cin >> arrival;
            cout << "修改成功!" << endl;
        }else if(input == "dprtime"){
            cout << "请输入新的发车时间:";
            Date dprtime;
            dprtime.setDate();
            if(dprtime.dateLaterThan(ticket->getArvtime())){
                cout << "发车时间不能晚于到达时间!" << endl;
            }else{
                ticket->setDprtime(dprtime);
                cout << "修改成功!" << endl;
            }
        }else if(input == "arvtime"){
            cout << "请输入新的到达时间:";
            Date arvtime;
            arvtime.setDate();
            if(ticket->getDprtime().dateLaterThan(arvtime)){
                cout << "发车时间不能晚于到达时间!" << endl;
            }else{
                ticket->setArvtime(arvtime);
                cout << "修改成功!" << endl;
            }
        }else if(input == "price"){
            cout << "请输入新的价格:";
            int price;
            cin >> price;
            ticket->setPrice(price);
            cout << "修改成功!" << endl;
        }else if(input == "remain"){
            cout << "请输入新的余票:";
            int remain;
            cin >> remain;
            ticket->setRemain(remain);
            cout << "修改成功!" << endl;
        }else{
            cout << "未找到相应信息!" << endl;
        }
    }else{
        cout << "未找到相应车次!" << endl;
    } 
    return true;
}

//查code
Ticket* TicketList::codeSearch(const string& code){
    for(auto it = tickets.begin(); it != tickets.end(); ++it){
        if(it->getCode()==code){
            return &(*it);  //不能直接返回it, 因为it是vector<Ticket>::const_iterator迭代器类型, it虽然是该迭代器目前所处的地址, 但是并不兼容指针Ticket*, 所以必须解引用再引用
        }
    }
    return nullptr;
}
/*
if(Ticket *t = ticketList.codeSearch("z123")){
...
}else{
...
}
*/



//查dpr
TicketList TicketList::dprSearch(const string& input){
    TicketList searchlist;
    for(auto train = tickets.begin(); train != tickets.end(); ++train){
        if(train->getDeparture() == input){
            searchlist.tickets.push_back(*train);
        }
    }
    return searchlist;
}

//查arv
TicketList TicketList::arvSearch(const string& input){
    TicketList searchlist;
    for(auto train = tickets.begin(); train != tickets.end(); ++train){
        if(train->getArrival() == input){
            searchlist.tickets.push_back(*train);
        }
    }
    return searchlist;
}

bool TicketList::bookTicket(OrderList& oManager){
    string code;
    cout << "请输入要购买的车次:";
    cin >> code;
    if(auto train = this->codeSearch(code)){
        if(train->getRemain() > 0){
            train->setRemain(train->getRemain() - 1);
            string passengerName;
            cout << "请输入乘客姓名:";
            cin >> passengerName;
            string passengerID;
            cout << "请输入乘客身份证号:";
            cin >> passengerID;
            string phoneNumber;
            cout << "请输入乘客电话号码:";       //目前没做正确信息的验证
            cin >> phoneNumber;
            Order curOrder(code, passengerName, passengerID, phoneNumber);
            oManager.addOrder(curOrder);
            cout << "购票成功! 您的订单号为: " << curOrder.getOrderID() << endl;
            return true;
        }else{
            cout << "车票已售完!" << endl;
            return false;
        }
    }else{
        cout << "车次不存在!" << endl;
        return false;
    }
}

void TicketList::saveTickets2File(){
    ofstream file("./files/tickets.txt");
    for(auto train : tickets){
        file << train.getCode() << "|" << train.getDeparture() << "|" << train.getArrival() 
             << "|" << train.getDprtime() << "|" << train.getArvtime() 
             << "|" << train.getPrice() << "|" << train.getRemain() << endl;
    }
}

void TicketList::loadTicketsFromFile(){
    ifstream file("./files/tickets.txt");
    if (!file.is_open()) {
        cerr << "无法打开文件 tickets.txt" << endl;
        return;
    }

    string line;
    string code, departure, arrival;
    Date dprtime, arvtime;
    int price, remain;
    char delim;
    while(getline(file, line)){
        stringstream ss(line);
        getline(ss, code, '|');
        getline(ss, departure, '|');
        getline(ss, arrival, '|');
        ss >> dprtime;    
        ss >> delim;
        ss >> arvtime;
        ss >> delim;
        ss >> price >> delim >> remain;

        Ticket t(code, departure, arrival, dprtime, arvtime, price, remain);
        tickets.push_back(t);
    }
}